﻿using CTP.CRM.Core.Common.Enums;
using CTP.CRM.Repository.Employee.Entitys;
using CTP.CRM.Repository.Employee.ValueObjects;
using CTP.CRM.Repository.Organization.Entitys;
using CTP.CRM.Repository.Position.Entitys;
using CTP.CRM.Repository.User.Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CTP.CRM.Repository.Employee.Services
{
    public class EmployeeRepository : BasicRepository<EmployeeEntity>, IEmployeeRepository, IScopedDependency
    {
       // private static readonly string CacheKey = RedisKeyManage.GetEmpListKey();

        public async Task<List<EmployeeLoginValueObject>> GetEmpLoginValueObjectAsync(int empId)
        {
            var result = await base.Context.Queryable<EmployeeEntity>()
                .LeftJoin<UserEntity>((emp, user) => emp.Id == user.EmployeeId)
                .LeftJoin<EmployeePositionEntity>((emp, user, ep) => emp.Id == ep.EmployeeId)
                .LeftJoin<PositionEntity>((emp, user, ep, pos) => pos.PositionId == ep.PositionId)
                .LeftJoin<OrganizationEntity>((emp, user, ep, pos, com) => emp.CompanyId == com.OrgId)
                .LeftJoin<OrganizationEntity>((emp, user, ep, pos, com, dept) => emp.DeptId == dept.OrgId)
                .Where((emp, user, ep, pos, com, dept) => emp.Id == empId && emp.JobStatus == JobStatus.在职)
                .Select((emp, user, ep, pos, com, dept) => new EmployeeLoginValueObject()
                {
                    UserId = user.Id,
                    EmployeeId = emp.Id,
                    EmployeeCode = emp.EmployeeCode,
                    EmployeeName = emp.Name,
                    EnName = emp.EnName,
                    CompanyId = emp.CompanyId,
                    CompanyName = com.CnName,
                    DeptId = emp.DeptId,
                    DeptName = dept.CnName,
                    PositionId = ep.PositionId,
                    PositionName = pos.Name,
                    IsDeleted = emp.IsDeleted,
                    JobStatus = emp.JobStatus,
                    IsMasterPosition = ep.IsMasterPosition
                }).ToListAsync();

            return result;
        }

  
    }

}
